package com.beiwang2;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "BeiWangDB.db"; //数据库名称
    private static final int version = 1; //数据库版本
    public static final String IMPTASK_TABLE = "Imptask";
    public static final String PLANTASK_TABLE = "Plantask";
    public static final String NOTEBOOK_TABLE = "Notebook";
    public static final String RECYCLEBIN_TABLE = "RecycleBin";
    public static final String REPORT_TABLE = "AccReport";
    public static final String MINELOG_TABLE = "MineLog";
    public static final String USER_TABLE = "user";
    public static final String DAILY_TABLE = "daily";
    public static final String REPORTBIN_TABLE = "ReportBin";
    public static final String MINELOGBIN_TABLE = "MineLogBin";

    public DBHelper(Context context) {
        super(context, DB_NAME, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table if not exists " + IMPTASK_TABLE +
                "( title text , task text, " +
                "datetime text, " +
                "impcode varchar(10)," +
                "checked int default 0," +
                "alarmid int" +
                ") ";
        String sql2 = "create table if not exists " + PLANTASK_TABLE +
                " (planid integer primary key AUTOINCREMENT, title text, " +
                "detail text, " +
                "img1 blob,"+
                "img2 blob,"+
                "img1uri text,"+
                "img2uri text,"+
                "startTime text,"+
                "plantime text,"+
                "endtime text,"+
                "planworktime double,"+
                "actuallyworktime double default 0,"+
                "isTimeout int default 0,"+
                " state int default 0, " +
                "note text"+
                ") ";
        String sql3 = "create table if not exists " + NOTEBOOK_TABLE +
                "( bookid integer primary key AUTOINCREMENT,"+
                "name text , tag varchar(256), " +
                "cover blob,"+
                "coveruri text,"+
                "creattime text, " +
                "isdelete int default 0 " +
                ") ";
        String sql4 = "create table if not exists " + RECYCLEBIN_TABLE +
                "( id integer primary key AUTOINCREMENT,"+
                "bookid integer , noteid integer, " +
                "title text, " +
                "time text " +
                ") ";
        String sql5 = "create table if not exists " + REPORT_TABLE +
                "( id integer primary key AUTOINCREMENT,"+
                "planid integer , title text, " +
                "report text, " +
                "time text, " +
                "fileurls text, " +
                "complete_time text ,"+
                "isdelete int default 0 "+
                ") ";
        String sql6 = "create table if not exists " + MINELOG_TABLE +
                "( id integer primary key AUTOINCREMENT,"+
                "workname text , work text, " +
                "unfinished text, " +
                "problem text, " +
                "imgurls text, " +
                "fileurls text, " +
                "time textm, "+
                "isdelete int default 0 "+
                ") ";
        String sql7 = "create table if not exists " + USER_TABLE +
                "(worktime double default 0 , timeout integer default 0, " +
                "complete integer default 0, " +
                "day integer default 0, " +
                "today text, " +
                "slogan text default null " +
                ") ";
        String sql8 = "create table if not exists " + DAILY_TABLE +
                "( id integer primary key AUTOINCREMENT,"+
                "date text, worktime double default 0 "+
                ") ";
        String sql9 = "create table if not exists " + REPORTBIN_TABLE +
                "( id integer primary key AUTOINCREMENT,"+
                "rpid integer, " +
                "title text, " +
                "time text " +
                ") ";
        String sql10 = "create table if not exists " + MINELOGBIN_TABLE +
                "( id integer primary key AUTOINCREMENT,"+
                "logid integer, " +
                "title text, " +
                "time text " +
                ") ";
        db.execSQL(sql);
        db.execSQL(sql2);
        db.execSQL(sql3);
        db.execSQL(sql4);
        db.execSQL(sql5);
        db.execSQL(sql6);
        db.execSQL(sql7);
        db.execSQL(sql8);
        db.execSQL(sql9);
        db.execSQL(sql10);
        ContentValues cv = new ContentValues();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cv.put("worktime",0);
        cv.put("timeout",0);
        cv.put("complete",0);
        cv.put("day",1);
        cv.put("today",df.format(new Date()));
        db.insert(USER_TABLE,null,cv);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + IMPTASK_TABLE;
        db.execSQL(sql);
        onCreate(db);
    }
}
